(import
 (except (rnrs base)
         let-values
         map)
 (only (guile)
       lambda* λ
       string-split)
 (fileio)
 ;; receive form
 (srfi srfi-1)
 (ice-9 pretty-print)
 )


(define make-range
  (λ (a b)
    (cons a b)))

(define string-range->range
  (λ (str-range)
    (let ([parts (string-split str-range #\-)])
      (make-range (string->number (car parts))
                  (string->number (car (cdr parts)))))))

(define range-start
  (λ (range)
    (car range)))

(define range-end
  (λ (range)
    (cdr range)))

(define range-contains-range?
  (λ (range1 range2)
    (and (<= (range-start range1) (range-start range2))
         (>= (range-end range1) (range-end range2)))))

(define count
  (λ (lst pred)
    (let iter ([counter 0] [lst° lst])
      (cond
       [(pred (car lst°))
        (iter (+ counter 1) (cdr lst°))]
       [else
        (iter counter (cdr lst°))]))))

(define lines (get-lines-from-file "input"))
(define line-ranges
  (map (λ (line) (string-split line (λ (c) (char=? #\, c))))
       lines))
(define ranges
  (map (λ (range-strs)
         (cons (string-range->range (car range-strs))
               (string-range->range (cadr range-strs))))
       line-ranges))

(define matches
 (map (λ (range-pair)
        (let ([range1 (car range-pair)]
              [range2 (cdr range-pair)])
          (or (range-contains-range? range1 range2)
              (range-contains-range? range2 range1))))
      ranges))

(pretty-print
 (fold (λ (flag acc)
         (+ acc (if flag 1 0)))
       0
       matches))
